package cn.juque.jdfsystem.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.juque.common.base.BaseRequestVO;
import cn.juque.common.base.BaseResponseVO;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.jdfsystem.domain.FileInfoDO;
import cn.juque.jdfsystem.entity.FileInfo;
import cn.juque.jdfsystem.service.IFileInfoService;
import cn.juque.jdfsystem.vo.fileinfo.FileInfoDetailVO;
import cn.juque.jdfsystem.vo.fileinfo.FileInfoListVO;
import cn.juque.jdfsystem.vo.fileinfo.FileInfoSearchVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>FileInfoController</li>
 * </ul>
 * @date 2023-01-09 11:07:04
 **/
@Slf4j
@Controller
@RequestMapping("/jdf/file")
public class FileInfoController extends BaseFileController {

    @Resource
    private IFileInfoService fileInfoService;

    /**
     * 查询
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/list")
    @ResponseBody
    public BaseResponseVO<List<FileInfoListVO>> list(@RequestBody BaseRequestVO<FileInfoSearchVO> request) {
        FileInfoSearchVO searchVO = super.validatorUtil.checkNotNull(request);
        PageInfo pageInfo = request.getPageInfo();
        super.validatorUtil.checkPageInfo(pageInfo);
        FileInfoDO fileInfo = new FileInfoDO();
        BeanUtil.copyProperties(searchVO, fileInfo);
        List<FileInfoDO> list = this.fileInfoService.listInfoWithPage(pageInfo, fileInfo);
        List<FileInfoListVO> voList = BeanUtil.copyToList(list, FileInfoListVO.class);
        return new BaseResponseVO<>(MessageEnum.OK, voList, pageInfo);
    }

    /**
     * 查询
     *
     * @param id 主键
     * @return BaseResponseVO
     */
    @GetMapping(value = "/detail")
    @ResponseBody
    public BaseResponseVO<FileInfoDetailVO> detail(String id) {
        Assert.notEmpty(id);
        FileInfo fileInfo = this.fileInfoService.detail(id, false);
        FileInfoDetailVO detailVO = new FileInfoDetailVO();
        BeanUtil.copyProperties(fileInfo, detailVO);
        return new BaseResponseVO<>(MessageEnum.OK, detailVO);
    }

    /**
     * 变更文件状态
     *
     * @param id 主键
     * @return BaseResponseVO
     */
    @GetMapping(value = "/trust")
    @ResponseBody
    public BaseResponseVO<Boolean> trustFile(String id, String platformId, Boolean flag) {
        Assert.notEmpty(id);
        Assert.notEmpty(platformId);
        if (Boolean.TRUE.equals(flag)) {
            this.fileInfoService.trustFile(platformId, CollUtil.newArrayList(id), null);
        } else {
            this.fileInfoService.trustFile(platformId, null, CollUtil.newArrayList(id));
        }
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }

    /**
     * 上传
     *
     * @throws IOException IOException
     */
    @PostMapping(value = "/upload")
    @ResponseBody
    public BaseResponseVO<String> uploadFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam("platformId") String platformId,
            @RequestParam("scope") Integer scope,
            @RequestParam("virtuallyPath") String virtuallyPath) throws IOException {
        String fileId = super.upload(file, platformId, scope, virtuallyPath);
        return new BaseResponseVO<>(MessageEnum.OK, fileId);
    }

    /**
     * 查看文件
     *
     * @param id         文件ID
     * @param platformId 平台ID
     * @param response   HttpServletResponse
     */
    @GetMapping(value = "/view/{id}")
    @Override
    public void view(@PathVariable String id, String platformId, String sign, Long timestamp, HttpServletResponse response) throws IOException {
        super.view(id, platformId, sign, timestamp, response);
    }

    @PostMapping(value = "/delete")
    @ResponseBody
    public BaseResponseVO<Boolean> del(@RequestBody BaseRequestVO<List<String>> request) {
        super.validatorUtil.fastValidate(request);
        this.fileInfoService.deleteInfo(request.getParam());
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }

    /**
     * 获取文件的详情
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/listFileInfo")
    @ResponseBody
    public BaseResponseVO<List<FileInfoDetailVO>> listFileInfo(@RequestBody BaseRequestVO<List<String>> request) {
        this.validatorUtil.checkNotNull(request);
        List<FileInfo> list = this.fileInfoService.detailFileByIds(request.getParam());
        CopyOptions copyOptions = CopyOptions.create(FileInfoDetailVO.class, true,
                "platformId", "serverPath", "contentType", "validFlag");
        List<FileInfoDetailVO> voList = BeanUtil.copyToList(list, FileInfoDetailVO.class, copyOptions);
        return new BaseResponseVO<>(MessageEnum.OK, voList);
    }
}
